home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 12551 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.3 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Need help with a STRING that won't go away!!!
  5. Date: Mon, 01 Apr 96 16:48:38 GMT
  6. Organization: none
  7. Message-ID: <828377318snz@genesis.demon.co.uk>
  8. References: <4jh7e2$jjr@abel.cc.sunysb.edu>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4jh7e2$jjr@abel.cc.sunysb.edu>
  15.            ghauser@ic.sunysb.edu "George Hauser" writes:
  16.  
  17. ...
  18.  
  19. >#include <stdio.h>
  20. >#include <string.h>
  21. >#include <stdlib.h>
  22. >
  23. >#define linemax 194    /* The predifined lenght of the record */
  24. >
  25. >--- All previous functions and file openings are coded here.
  26. >
  27. > records = 0;
  28.  
  29.  
  30. > while(!feof(sourcefp))
  31. > {
  32. >           if (ferror(sourcefp))
  33. >           {
  34. >                   printf("\nError while reading source file.\n");
  35. >                   exit(1);
  36. >           }
  37. >           fgets(line,linemax,sourcefp);        /* Get Bad Record */
  38.  
  39. The system isn't prescient - feof() and ferror() test whether the last
  40. operation resulted in and end-of-file or error condition, not whether the
  41. next one will do. Most stdio functions have a return value that indicates
  42. EOF/error - it is best to use it. Replace the section above with:
  43.  
  44.   while(fgets(line,linemax,sourcefp) != NULL)
  45.   {
  46.  
  47. >           rmgarbage(line);                     /* Remove bad chars */
  48. >           fillrecords(line);                   /* Format the record line */
  49. >
  50. >           /* here I tried checking for empty lines */
  51. >           fputs(line,destfp);                  /* Write to disk */
  52. >
  53. >           records++;                           /* Increment our counter */
  54. >
  55. >         /* here I was setting the line[] to '\0' */
  56. >
  57. > } 
  58.  
  59. And if you wish you can add here:
  60.  
  61.   if (ferror(sourcefp))
  62.   {
  63.         printf("\nError while reading source file.\n");
  64.         exit(1);
  65.   }
  66.  
  67. > fclose(sourcefp);  /* Close source file. */
  68. > fclose(destfp);    /* Close destination file. */
  69. > printf("Total Number Of Records Processed: %d \n", records);
  70. >} /* End */
  71. >
  72.  
  73. -- 
  74. -----------------------------------------
  75. Lawrence Kirby | fred@genesis.demon.co.uk
  76. Wilts, England | 70734.126@compuserve.com
  77. -----------------------------------------
  78.